/**
 * StreetNameParser.java
 * 
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fpdev.util;

import org.fpdev.util.FPUtil;
import org.fpdev.core.basenet.Locations;

public class StreetNameParser {

  private ParsedStreetName ssnOriginal_,  ssnResolved_;
  private boolean areDistinct_;

  public StreetNameParser(String fullname, Locations addrLookup) {
    this(fullname, addrLookup, false);
  }

  public StreetNameParser(String fullname, Locations addrLookup, boolean debug) {
    String dir, name, type;
    int quad;

    String st = FPUtil.standardizeStreetName(fullname, false);

    if (st.startsWith("e ") || st.startsWith("w ") || st.startsWith("n ") || st.startsWith("s ")) {
      dir = st.substring(0, 1);
      st = st.substring(2, st.length());
    } else {
      dir = "";
    }

    quad = FPUtil.getQuadrant(st);
    st = FPUtil.stripQuadrant(st);
    String[] strArr = st.split(" ");

    if (strArr.length == 1) {
      name = st;
      type = "";
    } else {
      String testType = strArr[strArr.length - 1];
      if (FPUtil.isValidStreetType(testType)) {
        type = testType;
        name = st.substring(0, st.length() - type.length() - 1);
      } else {
        name = st;
        type = "";
      }
    }
    String resolvedName = addrLookup.resolveStreetAlias(name);

    ssnOriginal_ = new ParsedStreetName(dir, name, type, quad);
    ssnResolved_ = new ParsedStreetName(dir, resolvedName, type, quad);

    areDistinct_ = (resolvedName.compareTo(name) != 0);

    if (debug) {
      System.out.println("");
      System.out.println("--------------");
      System.out.println("[" + fullname + "]");
      System.out.println("[" + dir + "]");
      System.out.println("[" + name + "]");
      System.out.println("[" + type + "]");
      System.out.println("[" + quad + "]");
      System.out.println("--------------");
    }
  }

  public boolean areDistinct() {
    return areDistinct_;
  }

  public ParsedStreetName getOriginal() {
    return ssnOriginal_;
  }

  public ParsedStreetName getResolved() {
    return ssnResolved_;
  }
}	













